<?php

declare(strict_types=1);

namespace App\Controller;

use App\Constants\SuccessCode;
use App\Job\PushGlobalNotificationJob;
use App\Model\IndikatorNewsCategory;
use App\Model\User;
use App\Resource\BaseResource;
use App\Service\JPushService;
use App\Service\QueueService;
use Hyperf\Context\Context;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\WebSocketServer\Sender;

class Test extends AbstractController
{
    /**
     * @var Sender
     */
    #[Inject]
    protected $sender;

    /**
     * @var QueueService
     */
    #[Inject]
    protected $queueServer;


    public function index()
    {
        $id = Context::getOrSet('id', uniqid());

        $id2 = \Hyperf\Coroutine\wait(function () {

            return Context::getOrSet('id', uniqid());
        });

        User::find(1);


        var_dump($id, $id2);
        return $id . '====' . $id2;
    }

    public function getChildren()
    {
        // $data = \App\Model\Relation\UserInvite::with('children')->where('uid', 184)->select(['uid', 'invitation_uid'])->first();

        $data = IndikatorNewsCategory::with('children')->select(['id', 'name', 'parent_id'])->first();

        return new BaseResource(SuccessCode::SUCCESS, $data);
    }

    public function jpushSend(RequestInterface $request)
    {
        $user = User::find($request->input('uid'));

        if ($user->register_id) {
            // 发送消息
            $params = [
                'platform' => 'all',
                'title' => '通知',
                'content' => '客服介入通知了',
                'type' => 2,
                'builderId' => 1,
                'extras' => ['path' => '/home'],
                'alias' => '',
                'registrationId' => [$user->register_id],
                'sound' => ''
            ];
            $res = JPushService::androidOrIosPushByAlias($params);

            return new BaseResource(SuccessCode::SUCCESS, $res);
        } else {

            return '用户 register_id 不存在';
        }
    }

    public function wsSend(RequestInterface $request)
    {
        $this->queueServer->push(PushGlobalNotificationJob::class, [
            'type' => $request->input('type'),
            'customer_id' => '',
        ]);
    }
}
